package com.autoswf.dispatcher
{
	import com.autoswf.core.Link;
	
	import flash.events.Event;
	
	import flexunit.framework.TestCase;

	public class RemoteDispatcherTest extends TestCase
	{
		public function RemoteDispatcherTest(methodName:String=null)
		{
			super(methodName);
		}
		
		
		private var remoteDispatcher:RemoteDispatcher = new RemoteDispatcher("remoteDispatcher");
		private var link:Link = new Link("sender");
		private var receiver:RemoteDispatcher = new RemoteDispatcher("receiver");
		
		private var testEvent:Event = new Event(Event.CHANGE);
		
		/* Test handler used in most tests */
		private var handler:Function = function(e:Event):void {	assertStrictlyEquals(e, testEvent);	}
		
		
		override public function setUp():void
		{
			link.addParentLink(remoteDispatcher);
			receiver.addParentLink(remoteDispatcher);
		}
		
		
		/** Tests that a remote dispatcher will listeners from the network. */
		public function test01():void
		{
			link.sendUp(new ListenerTransport( [Event.CHANGE, new Function] ));
			
			assertTrue(remoteDispatcher.hasEventListener(Event.CHANGE));
		}


		/** Tests that a remote dispatcher will remove listeners. */
		public function test02():void
		{
			link.sendUp(new ListenerTransport( [Event.CHANGE, handler] ));
			link.sendUp(new ListenerTransport( [Event.CHANGE, handler], true ));
			
			assertFalse(remoteDispatcher.hasEventListener(Event.CHANGE));
		}
				
		
		/**
		 * Tests that a remote dispatcher link will register listeners passed up, and that they 
		 * will get triggered if matching events are passed up. 
		 */
		public function test03():void
		{
			link.sendUp( new ListenerTransport([Event.CHANGE, addAsync(handler, 20)]) );
			
			link.sendUp(testEvent);
		}
		
		
		/** Tests that addRemoteListener will add a listener to the remote dispatcher. */
		public function test04():void
		{
			receiver.addRemoteListener(Event.CHANGE, new Function);
			
			assertTrue(remoteDispatcher.hasEventListener(Event.CHANGE));
		}
		

		/** Tests that removeRemoteListener will remove a listener from the remote dispatcher. */
		public function test05():void
		{
			receiver.addRemoteListener(Event.CHANGE, handler);
			receiver.removeRemoteListener(Event.CHANGE, handler);
			
			assertFalse(remoteDispatcher.hasEventListener(Event.CHANGE));
		}
		
		
		/**
		 * Tests that addRemoteListener results in an invoked handler when events of that type 
		 * pass through the remote dispatcher. 
		 */
		public function test06():void
		{
			receiver.addRemoteListener(Event.CHANGE, addAsync(handler, 20));
			link.sendUp(testEvent);
		}
		
		
		/** Verifies that removeRemoteListener removes the listener from the remote dispatcher. */
		public function test07():void
		{
			var handler:Function = function(e:Event):void
			{
				assertNull("RemoteDispatcherTest.test07()", e);
			}
			
			receiver.addRemoteListener(Event.CHANGE, handler);
			receiver.removeRemoteListener(Event.CHANGE, handler);
			link.sendUp(testEvent);
		}
		
	}
}